package flexulous;

/*
 * Flexulous.java
 * part of the Flexulous plugin for the jEdit text editor
 * Copyright (C) 2001 Chris Hill
 * jgellene@nyc.rr.com
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Flexulous.java 12504 2008-04-22 23:12:43Z ezust $
 */

// {{{ imports
import flexulous.model.FlexulousModel;
import flexulous.view.FlexulousTargetsPanel;
import flexulous.view.FlexulousToolPanel;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;

import javax.swing.JPanel;

import org.gjt.sp.jedit.EBComponent;
import org.gjt.sp.jedit.EBMessage;
import org.gjt.sp.jedit.EditBus;
import org.gjt.sp.jedit.View;
import org.gjt.sp.jedit.jEdit;
import org.gjt.sp.jedit.gui.DefaultFocusComponent;
import org.gjt.sp.jedit.gui.DockableWindowManager;
import org.gjt.sp.jedit.msg.PropertiesChanged;
import org.gjt.sp.util.StandardUtilities;

import flexulous.*;
import flexulous.view.*;
import flexulous.model.*;
import flexulous.util.*;


// }}}

// {{{ Flexulous class
/**
 * 
 * Flexulous - a dockable JPanel, a demonstration of a jEdit plugin.
 *
 */
public class Flexulous extends JPanel
    implements EBComponent, DefaultFocusComponent {

    // {{{ Instance Variables
	private static final long serialVersionUID = 7412255692894321789L;

	private String filename;

	private String defaultFilename;

	private View view;

	private boolean floating;

	private FlexulousToolPanel toolPanel;
    
    private FlexulousTargetsPanel targetsPanel;
	
	private FlexulousModel model;
	
    // }}}

    // {{{ Constructor
	/**
	 * 
	 * @param view the current jedit window
	 * @param position a variable passed in from the script in actions.xml,
	 * 	which can be DockableWindowManager.FLOATING, TOP, BOTTOM, LEFT, RIGHT, etc.
	 * 	see @ref DockableWindowManager for possible values.
	 */
	public Flexulous(View view, String position) {
		super(new BorderLayout());
		model = FlexulousPlugin.getModel();
		if(model == null){
			throw new RuntimeException("Hmmm...flexulous has no model!");
		}
		this.view = view;
		this.floating = position.equals(DockableWindowManager.FLOATING);

		if (jEdit.getSettingsDirectory() != null) {
			this.filename = jEdit.getProperty(FlexulousPlugin.OPTION_PREFIX
					+ "sdklocation");
			if (this.filename == null || this.filename.length() == 0) {
				this.filename = new String(jEdit.getSettingsDirectory()
						+ File.separator + "qn.txt");
				jEdit.setProperty(
						FlexulousPlugin.OPTION_PREFIX + "sdklocation",
						this.filename);
			}
			this.defaultFilename = this.filename;
		}

		this.toolPanel = new FlexulousToolPanel();
        this.targetsPanel = new FlexulousTargetsPanel(this, model);
		add(BorderLayout.NORTH, this.toolPanel);
        add(BorderLayout.CENTER, this.targetsPanel);
        
		if (floating)
			this.setPreferredSize(new Dimension(500, 250));
	}
    // }}}

    // {{{ Member Functions
    
    // {{{ focusOnDefaultComponent
	public void focusOnDefaultComponent() {
	}
    // }}}

    // {{{ getFileName
	public String getFilename() {
		return filename;
	}
    // }}}

	// EBComponent implementation
	
    // {{{ handleMessage
	public void handleMessage(EBMessage message) {
		if (message instanceof PropertiesChanged) {
			propertiesChanged();
		}
	}
    // }}}
    
    // {{{ propertiesChanged
	private void propertiesChanged() {
		String propertyFilename = jEdit
				.getProperty(FlexulousPlugin.OPTION_PREFIX + "sdklocation");
		if (!StandardUtilities.objectsEqual(defaultFilename, propertyFilename)) {
			toolPanel.propertiesChanged();
			defaultFilename = propertyFilename;
			filename = defaultFilename;
		}
	}
    // }}}

	// These JComponent methods provide the appropriate points
	// to subscribe and unsubscribe this object to the EditBus.

    // {{{ addNotify
	public void addNotify() {
		super.addNotify();
		EditBus.addToBus(this);
	}
     // }}}
     
    // {{{ removeNotify
	public void removeNotify() {
		super.removeNotify();
		EditBus.removeFromBus(this);
	}
    // }}}
    
}
// }}}
